package doan.chuyennganh.activity;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import doan.chuyennganh.base.DoAnBaseAcitvity;
import doan.chuyennganh.model.Attendance;
import doan.chuyennganh.model.AttendanceList;
import doan.chuyennganh.model.ClassStudent;
import doan.chuyennganh.model.ListClass;
import doan.chuyennganh.model.ListStudent;
import doan.chuyennganh.model.Student;
import doan.chuyennganh.util.Constants;
import doan.chuyennganh.view.SearchingView;

public class ListStudentActivity extends DoAnBaseAcitvity {
	private SearchingView searchText;
	private ProgressDialog prog;
	private ImageView btnSearch;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		prog = new ProgressDialog(ListStudentActivity.this);
		prog.setMessage(getString(R.string.txt_loading));
		prog.setCancelable(false);
		prog.show();
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				
				init();
			}
		}, 1000);
		
	}

	public void init() {
		setContentView(R.layout.list_student_activity);
		searchText = (SearchingView) findViewById(R.id.searching_view);
		searchText.setTextHint(getString(R.string.txt_search_student_by_id));
		btnSearch = (ImageView) searchText.findViewById(R.id.search_edittext_imv);
		btnSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String id = searchText.getText();
				Student student = ListStudent.getInstance().getStudent(id);
				if(student != null){
					AttendanceList attendanceList = ListStudent.getInstance()
							.getAttendanceList();
					int period = 0;
					for (int k = 0; k < attendanceList.size(); k++) {
						Attendance tmp = attendanceList.get(k);
						period += tmp.get(id);
					}
					ClassStudent cls = ListClass.getInstance()
							.getClass((ListStudent.getInstance()
									.getClassID()));
					String format = "%s/%s tiết";
					String currentRate =  String.format(format, period, cls.getTongsotiet());
					enableDialogWarning(ListStudentActivity.this, getString(R.string.txt_info), "Tên: " + student.getMaSV() + "\nLớp: " + student.getLop() + "\nNghỉ: " + currentRate, false);
				}else{
					enableDialogWarning(ListStudentActivity.this, getString(R.string.txt_info), getString(R.string.txt_not_found), false);
				}
			}
		});
		if (ListStudent.getInstance() != null) {

			ArrayList<Student> list = ListStudent.getInstance()
					.getAllStudents();
			AttendanceList attendanceList = ListStudent.getInstance()
					.getAttendanceList();
			if (list != null) {
				ArrayList<String> items = new ArrayList<String>();
				for (Student st : list) {
					items.add(st.getMaSV() + "-" + st.getHoten() + "("
							+ st.getLop() + ")");
				}
			}

			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			ScrollView sctable = (ScrollView) findViewById(R.id.scroll_list_students);
			HorizontalScrollView hScrollView= new HorizontalScrollView(this);
			hScrollView.setLayoutParams(layoutParams);
			sctable.setVerticalScrollBarEnabled(true);
			sctable.setHorizontalScrollBarEnabled(true);
			sctable.setLayoutParams(layoutParams);
			// HorizontalScrollView scHorizontalScrollView = new
			// HorizontalScrollView(this);
			TableRow.LayoutParams params = new TableRow.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
			TableLayout table = new TableLayout(this);
			table.setBackgroundResource(R.color.white);
			table.setPadding(0, 0, 0, (int) convertDpToPixel(60, this));
			TableRow cot = new TableRow(this);
			cot.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.MATCH_PARENT));
			TextView tvmasv = new TextView(this);
			tvmasv.setGravity(Gravity.CENTER);
			tvmasv.setTextColor(Color.BLUE);
			tvmasv.setBackgroundResource(R.drawable.boder_backgroud);
			tvmasv.setLayoutParams(params);
			tvmasv.setText("MSSV");
			TextView tvhoten = new TextView(this);
			tvhoten.setBackgroundResource(R.drawable.boder_backgroud);
			tvhoten.setTextColor(Color.BLUE);
			tvhoten.setLayoutParams(params);
			tvhoten.setText("Họ tên");
			tvhoten.setGravity(Gravity.CENTER);
			TextView tvmalop = new TextView(this);
			tvmalop.setGravity(Gravity.CENTER);
			tvmalop.setBackgroundResource(R.drawable.boder_backgroud);
			tvmalop.setTextColor(Color.BLUE);
			tvmalop.setLayoutParams(params);
			tvmalop.setText("Lớp");
			TextView tvtilenghi = new TextView(this);
			tvtilenghi.setTextColor(Color.BLUE);
			tvtilenghi.setLayoutParams(params);
			tvtilenghi.setText("Nghỉ");
			tvtilenghi.setPadding(5, 5, 5, 5);
			tvtilenghi.setBackgroundResource(R.drawable.boder_backgroud);
			tvtilenghi.setGravity(Gravity.CENTER);
			cot.addView(tvmasv);
			cot.addView(tvhoten);
			cot.addView(tvmalop);
			cot.addView(tvtilenghi);
			table.addView(cot);
			for (int i = 0; i < list.size(); i++) {
				TableRow row = new TableRow(this);
				row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.MATCH_PARENT));
				for (int j = 0; j < 4; j++) {
					TextView text = new TextView(this);
					text.setPadding(5, 5, 5, 5);
					text.setBackgroundResource(R.drawable.boder_backgroud);
					text.setGravity(Gravity.CENTER);
					text.setLayoutParams(params);
					text.setTextColor(Color.BLACK);
					// text.setText(String.format("(++)"));
					if (j == 0) {
						text.setText(list.get(i).getMaSV());
					}

					else if (j == 1) {
						text.setGravity(Gravity.LEFT);
						text.setText(list.get(i).getHoten());
					} else if (j == 2) {
						text.setText(list.get(i).getLop());
					} else if(j == 3){
						int period = 0;
						for (int k = 0; k < attendanceList.size(); k++) {
							Attendance tmp = attendanceList.get(k);
							period += tmp.get(list.get(i).getMaSV());
						}
						ClassStudent cls = ListClass.getInstance()
								.getClass((ListStudent.getInstance()
										.getClassID()));
						int maxRate = Constants.BANNED_RATE;
						text.setText("0%");
						text.setTextColor(Color.BLUE);
						if (cls != null) {
							int currentRate = (int) Math
									.round((double) period / cls
											.getTongsotiet() * 100);
							text.setText(currentRate + "%");
							if(currentRate >= maxRate){
								text.setTextColor(Color.RED);
							}
						}
					}
					row.addView(text);
				}

				table.addView(row);
			}
			table.setGravity(Gravity.CENTER_HORIZONTAL);
			table.setLayoutParams(new LayoutParams(
					TableLayout.LayoutParams.MATCH_PARENT,
					TableLayout.LayoutParams.MATCH_PARENT));

			hScrollView.addView(table);
			sctable.addView(hScrollView);
		}
		if(prog != null && prog.isShowing()){
			prog.dismiss();
			prog = null;
		}
	}
}
